package com.hyy.service.serviceImpl;

import com.hyy.bean.Cart;
import com.hyy.bean.Merchandise;
import com.hyy.dao.CartDao;

import com.hyy.dao.MerchandiseDao;
import com.hyy.dao.daoImpl.CartDaoImpl;
import com.hyy.dao.daoImpl.MerchandiseDaoImpl;
import com.hyy.service.CartService;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CartServiceImpl implements CartService {

	CartDao cartDao = new CartDaoImpl();
	MerchandiseDao merchandiseDao = new MerchandiseDaoImpl();

	@Override
	public boolean insert(Cart cart) {
		try {
			return cartDao.insert(cart);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean deleteMyCart(Long uid,Long mid) {
		try {
			return cartDao.deleteMyCart(uid,mid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean updateNum(Cart cart) {
		try {
			return cartDao.updateNum(cart);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public Map<Merchandise,Long> showAllByUid(long uid) {
		try {
			Map<Merchandise,Long> cartmerchandises = new HashMap<>();
			List<Cart> carts = cartDao.showAllByUid(uid);
			for (Cart cart : carts) {
				cartmerchandises.put(merchandiseDao.findProductById(cart.getMid()),cart.getNum());
			}
			return cartmerchandises;

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean addOne(Long uid, Long mid) {
		try {
			cartDao.addNum(uid,mid,1);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
}
